package cn.edu.nju.gitlab.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.nju.gitlab.R;

import cn.edu.nju.gitlab.injector.DaggerActivityComponent;
import cn.edu.nju.gitlab.injector.MainModule;
import cn.edu.nju.gitlab.model.Group;
import cn.edu.nju.gitlab.model.User;
import cn.edu.nju.gitlab.presenter.StudentPresenter;


/**
 * Created by lpt on 2017/6/3.
 */

public class StudentActivity extends AppCompatActivity {
    private Group group;
    private List<Map<String, Object>> studentMap;
    private List<User> userList;

    @BindView(R.id.list)
    ListView listView;

    @Inject
    StudentPresenter presenter;
    private String username;
    private String password;
    private int groupId;
    private int userId;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();
        username = intent.getStringExtra("username");
        password = intent.getStringExtra("password");
        userId = intent.getIntExtra("userId", 0);
        type = intent.getStringExtra("type");
        group = (Group) intent.getSerializableExtra("group");
        groupId = group.getId();
        setContentView(R.layout.student_list);
        ButterKnife.bind(this);
        DaggerActivityComponent.builder()
                .mainModule(new MainModule(this))
                .build()
                .inject(this);
        presenter.getStudents(username,password,groupId);

    }

    public void showError(){
        System.out.println("error");
    }

    public void showList(List<User> students) {
        studentMap = new ArrayList<>();
        userList= students;
        for (User user : userList) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", "姓名：" + user.getName());
            map.put("content", "学号：" + user.getNumber());
            studentMap.add(map);
        }

//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(view.getContext(), QuestionActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("question", questionList.get(position));
//                intent.putExtras(bundle);
//                startActivity(intent);
//            }
//        });


        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                String[] from = {"content", "title"};
                int[] to = {R.id.title, R.id.content};
                SimpleAdapter adapter = new SimpleAdapter(listView.getContext(), studentMap, R.layout.student_info, from, to);
                listView.setAdapter(adapter);
            }
        };
        runOnUiThread(myRunnable);
    }

}
